<?php
/**
 * 合同模块model
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/9 0009
 * Time: 下午 2:35
 */

namespace App\Model\Backstage;

use App\Model\Base;
use App\Model\Account\Type;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class Contract extends Base
{

    protected $table;
    public function __construct()
    {
        parent::__construct();
//        $this->table = 'contract_'.$this->branch_id;
        $this->table = 'contract';
    }



    /** 查询列表
     * @param $map
     * @return mixed
     */
    public function getList($map=[])
    {
        $list = DB::table($this->table)
            ->leftJoin('user as ua', 'signatory', '=', 'ua.u_id')
            ->leftJoin('user as ub', 'signatory_director', '=', 'ub.u_id')
            ->leftJoin('department as da', 'signatory_department', '=', 'da.id')
            ->leftJoin('department as db', 'signatory_director_department', '=', 'db.id')
            ->where($map)
            ->select($this->table.'.*', 'ua.u_username as signatoryName', 'ub.u_username as signatory_departmentName',
                'da.name as signatory_directorName', 'db.name as signatory_director_departmentName')
            ->orderBy($this->table.'.id', 'desc')
            ->paginate(env('PAGE_LIMIT_ADMIN'));
        if (sizeof($list) == 0) {
            return false;
        }
        return $list;
    }

    /**  添加
     * @param $data
     * @return mixed
     */
    public function add($data)
    {
        $data['created_at'] = time();
        $data['updated_at'] = time();
        $number = DB::table($this->table)->where('number',$data['number'])->value('number');
        if(!isset($number)){
            return DB::table($this->table)
                ->insertGetId($data);
        }else{
            return false;//合同编号重复
        }


    }

    /** 修改
     * @param $data
     */
    public function edit($data)
    {
        $id = $data['id'];
        $data['updated_at'] = time();
        unset($data['id']);
         unset($data['number']);
        //dd($data);
        return DB::table($this->table)
            ->where('id', $id)
            ->update($data);
    }

    /** 删除
     * @param $id
     * @return mixed
     */
    public function deletes($id)
    {
        return DB::table($this->table)
            ->where('id', $id)
            ->delete();
    }

}